package com.naiterui.ehp.bs.cms.modules.pharmacy.controller;

import java.util.List;
import java.util.Objects;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.common.service.ICityService;
import com.naiterui.ehp.bs.cms.modules.common.vo.CityVO;
import com.naiterui.ehp.bs.cms.modules.pharmacy.entity.Warehouse;
import com.naiterui.ehp.bs.cms.modules.pharmacy.feign.DsWarehouseFeignClient;
import com.naiterui.ehp.bs.cms.modules.pharmacy.feign.WarehouseFeignClient;
import com.naiterui.ehp.bs.cms.modules.pharmacy.service.IWarehouseService;
import com.naiterui.ehp.bs.cms.modules.pharmacy.vo.WarehouseManageVO;
import com.naiterui.ehp.bs.cms.modules.pharmacy.vo.WarehouseSelectVO;
import com.naiterui.ehp.bs.cms.modules.pharmacy.vo.WarehouseVO;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Api(tags = {SwaggerTag.CMS + SwaggerTag.PHARMACY})
@RestController
@RequestMapping("pharmacy/warehouse")
public class WarehouseController {

  @Autowired
  private IWarehouseService warehouseService;

  @Autowired
  private ICityService cityService;

  @Autowired
  private WarehouseFeignClient warehouseFeignClient;

  @Autowired
  private DsWarehouseFeignClient dsWarehouseFeignClient;

  /**
   * 列表
   *
   * @return
   */
  @SysLog("商家列表")
  @GetMapping("/list")
  @ApiOperation("商家列表")
  @PreAuthorize("@p.check('pharmacy:partner:list')")
  public PageVO<WarehouseVO> list(PageParamsVO pageParamsVO, WarehouseVO warehouse) {
    return this.warehouseService.queryPage(pageParamsVO, warehouse);
  }

  @SysLog("下拉选择商家")
  @GetMapping("/select")
  @ApiOperation("下拉选择商家")
  public List<WarehouseSelectVO> selectList() {
    return this.warehouseService.getSelectList();
  }

  @SysLog("商家信息")
  @GetMapping("/{id}")
  @ApiOperation("商家信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "商家ID", dataType = "int", paramType = "path", required = true),
  })
  @PreAuthorize("@p.check('pharmacy:partner:info')")
  public Warehouse get(@PathVariable("id") Long id) {
    return this.warehouseService.getById(id);
  }

  /**
   * 修改商家
   *
   * @return
   */
  @SysLog("保存商家")
  @PostMapping("/save")
  @ApiOperation("保存商家")
  @PreAuthorize("@p.check('pharmacy:partner:save')")
  public Long save(@Valid @RequestBody String json) throws BusinessException {
    log.info("保存商家 请求参数：json = {}", json);
    Long warehouseId = this.warehouseFeignClient.saveOrUpdate(json);
    JSONObject jsonObject = JSONUtil.parseObj(json);
    WarehouseManageVO warehouseManage = new WarehouseManageVO();
    warehouseManage.setUserName(jsonObject.getStr("name"));
    warehouseManage.setPhone(jsonObject.getStr("phone"));
    if (jsonObject.containsKey("password")) {
        warehouseManage.setPassword(jsonObject.getStr("password"));
    }
    dsWarehouseFeignClient.manage(warehouseId, warehouseManage);
    return warehouseId;
  }

  /**
   * 修改商家
   */
  @SysLog("修改商家")
  @PostMapping("/update")
  @ApiOperation("修改商家")
  @PreAuthorize("@p.check('pharmacy:partner:update')")
  public void update(@Valid @RequestBody String json) throws BusinessException {
    Long id = this.warehouseFeignClient.saveOrUpdate(json);
    this.dsWarehouseFeignClient.manage(id, WarehouseManageVO.builder().phone(JSONUtil.parseObj(json).getStr("phone")).build());

  }

  @SysLog("设置修改管理员账户密码")
  @PostMapping("/manage/{id}")
  @ApiOperation("修改商家")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "商家ID", dataType = "int", paramType = "path", required = true),
  })
  @PreAuthorize("@p.check('pharmacy:partner:update')")
  public void manage(@PathVariable("id") Long id, @Valid @RequestBody WarehouseManageVO manageVO) throws BusinessException {
    this.dsWarehouseFeignClient.manage(id, manageVO);
  }

  /**
   * 修改商家状态
   */
  @SysLog("修改商家状态")
  @ApiOperation("修改商家状态")
  @GetMapping("/update/{id}/{status}")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "商家ID", dataType = "int", paramType = "path", required = true),
      @ApiImplicitParam(name = "status", value = "状态:是否停用 0:启用。1:停用", dataType = "int", paramType = "path", required = true)
  })
  @PreAuthorize("@p.check('pharmacy:partner:update')")
  public void updateStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) throws BusinessException {
    this.dsWarehouseFeignClient.updateStatus(id, status);
    this.warehouseFeignClient.updateStatus(id, status);
  }

  /**
   * 城市选择
   *
   * @return
   */
  @SysLog("城市选择")
  @GetMapping("/city/list")
  @ApiImplicitParam(name = "type", value = "类型:0:全国 1:省 2;市 3:区", dataType = "int", paramType = "query",
      required = true)
  @PreAuthorize("@p.check('pharmacy:partner:city:list')")
  public List<CityVO> list(@RequestParam(value = "type", required = false) String type) {
      // 如果为null则查全部
      if (Objects.isNull(type)) {
          type = "all";
      }
      return this.cityService.queryCityList(type);
  }

}
